package ran.quzitech.rnutils.manager;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import java.io.File;


/**
 * Created by Administrator on 2016/12/2 0002.
 */
public class ShareManager extends ReactContextBaseJavaModule {

    private Context context;
    private ReactContext reactContext;

    private static final String LOG_TAG = "snooker_ShareManager";


    @Override
    public String getName() {
        return "ShareManager";
    }

    public ShareManager(ReactApplicationContext reactContext) {
        super(reactContext);
        context = reactContext;
        this.reactContext = reactContext;


    }

    @ReactMethod
    public void sharePic(String path, Callback callback) {

        shareSingleImage(path);
        callback.invoke();
    }

    //分享单张图片
    public void shareSingleImage(String path) {
        //  String imagePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";
        //由文件得到uri
        Uri imageUri = Uri.fromFile(new File(path));
        Log.d("share", "uri:" + imageUri);  //输出：file:///storage/emulated/0/test.jpg

        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
        shareIntent.setType("image/*");
        context.startActivity(shareIntent);
        //Intent.createChooser(shareIntent, "分享到").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    }
}
